#!/bin/sh

# this script should be run in a folder, where repo and alps directory are needed

if [  ! -d alps ]; then
	echo "alps folder not found"
	exit -1
	if [ ! -d alps/.git ]; then
		 echo "alps not a git "
		 exit -1
	fi
fi

if [  ! -d repo ]; then
	echo "repo folder not found"
	exit -1
	if [ ! -d repo/.repo ]; then
		 echo "repo not a repo "
		 exit -1
	fi
fi


# read version info
ver_path=$(readlink -f repo/.repo/manifest.xml)
ver_name=${ver_path##*/}
echo $ver_name
cd alps
#
git checkout master && git clean -fd && git pull origin master
if git log -1 --oneline | grep -q $ver_name ; then
	echo "Found, do nothing"
else
	echo "No found, add a commit"
	cd -
	rsync -avh --delete-before --exclude .git --exclude .gitignore --exclude .repo repo/ alps
	cd alps
	git add -A && git commit -m "Version:$ver_name" && git push origin master:master

fi
lcid=$(git log -1 --oneline | cut -d' ' -f1)
git checkout BND && git pull -r origin BND && git clean -fd
cm=$(git log --oneline --grep=$ver_name)
if [ -z "$cm" ]; then
	git cherry-pick $lcid
	if [ $? -ne 0 ]; then
		echo "Fix conflict"
		git status ./
	else
		echo "No confict, just."
	fi
else
	echo "$ver_name already commit"
fi
cd -